/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.entidades;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.content.Context;
import android.content.ContextWrapper;
import static android.entidades.Usuario.id;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

/**
 *
 * @author efraingutierrez
 */
public class Cronometro 
{
    private static final String userFile = "cronometroV.txt";
    
    public static boolean existeArchivo()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            myReader.close();
            return true;
        } 
        catch (Exception e) {
            return false;
        }
    }
    
    public static int getMinutos()
    {
        if(existeArchivo())
        { 
            ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
            File directory = cw.getDir("media", Context.MODE_PRIVATE);
            String path = directory.getAbsolutePath()+"/"+userFile;
            File myFile;
            FileInputStream fIn;
            BufferedReader myReader = null;
            try {
                myFile = new File(path);
                fIn = new FileInputStream(myFile);
                myReader = new BufferedReader(new InputStreamReader(fIn));
                String aDataRow = "";
                String aBuffer = "";
                while ((aDataRow = myReader.readLine()) != null) 
                {
                    aBuffer += aDataRow;
                }
                myReader.close();
                return Integer.parseInt(aBuffer);
            } 
            catch (Exception e) 
            {
                return 5;
            }
        }
        else
        {
            updateCronometro(5);
            return getMinutos();
        }
    }
    
    //recibe param en minutos
    public static void updateCronometro(int minutos) 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter);
            out.write(""+minutos);
            out.write("\n");
            out.close();
        } 
        catch (Exception e) {
            Mensaje.Mostrar(e.toString());
        }
    }
      
    public static void delete() 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            File myFile = new File(path);
            myFile.delete();
            id = "";
        } 
        catch (Exception e) {
          Mensaje.Mostrar(e.toString());
        }
    }
}
